package munyorderweb;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import munyorderweb_DB.Change_Bean;

public class Table_conf extends HttpServlet {

	String sql;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	protected void order(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");

		System.out.println(req.getParameter("Yes"));
		ArrayList<Change_Bean> list = new ArrayList<Change_Bean>();
		Change_Bean change = null;

		int i;

		list = new ArrayList<Change_Bean>();
		String[] T_no = req.getParameterValues("no");
		String[] T_name = req.getParameterValues("name");
		String[] T_seats = req.getParameterValues("seats");
		for(i = 0; i<T_no.length; i++){
			System.out.println(T_no[i]);
			change = new Change_Bean();
			if(T_no != null && T_name != null && T_seats != null){
				if(T_no[i] != "" && T_name[i] != "" && T_seats[i] != ""){
					change.setTable_no(Integer.parseInt(T_no[i]));
					change.setTable_name(T_name[i]);
					change.setTable_seats(Integer.parseInt(T_seats[i]));
				}else{
					change.setTable_no(Integer.parseInt(T_no[i]));
					change.setTable_name("");
					change.setTable_seats(0);
				}
			}
			list.add(change);
		}
		if(req.getParameter("newName") != "" && req.getParameter("newSeats") != ""){
			change = new Change_Bean();
			String newName = req.getParameter("newName");
			String newSeats = req.getParameter("newSeats");
			change.setTable_no(0);
			change.setTable_name(newName);
			change.setTable_seats(Integer.parseInt(newSeats));
			list.add(change);
		}

		HttpSession session = req.getSession();
		session.setAttribute("list", list);

		//表示用Jspファイルを呼出して画面上に表示
		req.setAttribute("Table_conf", list);
		RequestDispatcher dispatcher = req.getRequestDispatcher("Table_conf.jsp");
		dispatcher.include(req, resp);
	}
}
